


//初始化客户归属下拉列表
ascription()
function ascription() {
    $.ajax({

        url:root.dockingplatform.listChannelConfig,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":20000000,
            "customerId":"",
            "customerName":"",
        }),
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data.result;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}



var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

function goodsList(data) {
    console.log(data)
    if(dataSource.length>0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if(data.data.listData.data!=null){
        $.each(data.data.listData.data, function(i, field) {
            var rowData = new Array();
            rowData.push(field.skuCode);
            rowData.push(field.imageUrl == null ? "" : field.imageUrl);
            rowData.push(field.skuName);
            rowData.push(field.color)
            rowData.push(field.specifications);
            rowData.push(field.brandName);
            rowData.push(field.displayPrice);
            rowData.push(field.settlementPrice);
            rowData.push(field.lastModifiedDate);
            rowData.push(field.unit);
            dataSource.push(rowData);

        });
    }

    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                "visible": true,
                "targets": 0
            },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },


                {
                    "render": function(data, type, row, meta) {
                        if(row[1] != '') {
                            var pp=row[1]+'?x-oss-process=style/style150'
                            var val = '<span id="spuImg1" ><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+pp+'\')" src="'+pp+'" width="80" height="40"></span>';
                            return val;
                        }else {
                            var val = '<span></span>';
                            return val;
                        }
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val =""
                        if(row[7]==null){
                            val =""
                        }else {
                            val = row[7]+'元/'+row[9]
                        }

                        return val;
                    },
                    "targets": 6
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[8];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":7
                },

            ],

        });

    };
}

/*表单验证特殊字符*/
function ValidateValue(textbox) {
    var IllegalString = "[`~!#$^&*()=|{}':;',\\[\\].<>/?~！#￥……&*（）——|{}【】‘；：”“'。，、？]‘’";
    var textboxvalue = textbox.value;
    var index = textboxvalue.length - 1;
    var s = textbox.value.charAt(index);
    if(IllegalString.indexOf(s) >= 0) {
        s = textboxvalue.substring(0, index);
        textbox.value = s;
        layer.msg("不能输入特殊字符");
    }
}

function goods_listCategory(categoryLevel) {
    console.log($("#class1").val())
    var categoryName = $("#class1").val();
    var user_data = {
        categoryLevel: categoryLevel,
        categoryName: $("#class1").val(),
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null) {
                classStr = "<li>无数据</li>"
                $("#classUl1").html(classStr);
            } else {
                var classStr = "";
                for(var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
                $("#classUl1").html(classStr);
            }
        }
    }
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

function picShow(title, url, imgUrl) {
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}
//获取光标
$("#class1").focus(function() {
    $("#classUl1").show();
    goods_listCategory(1);
});
$(document).bind("click", function(e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if($(e.target).closest("#classUl1").length == 0 && $(e.target).closest("#class1").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#classUl1").hide()
        $("#class2").val();

    }
});
var goodslist_cantegory_id; //定义分类搜索条件

var firstCategoryId;
var secondCategoryId;
var thirdCategoryId;

// 2,3,4级的模糊查询
function getNext_input(level, category_id) {
    if(level == 4) {
        goodslist_cantegory_id = category_id;
        thirdCategoryId = category_id;
    }
    if(level < 4) {
        console.log(level)
        $("#class" + level + "")[0].oninput = function() {
            var user_data = {
                parentCateId: category_id,
                categoryName: $("#class" + level + "").val()
            }
            var succCallBack = function(data, status, response) {
                if(data.errorCode == 0) {
                    if(data.data == null) {
                        classStr = "<li>无数据</li>";
                    } else {
                        var classStr = "";
                        for(var i = 0; i < data.data.length; i++) {
                            classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                        }
                    }
                    if(data.data[0] == undefined) {
                        classValue = category_id;
                        var nextLevel = level + 1;
                        $("#classUl" + nextLevel + "").html(classStr);
                        $("#classUl" + level + "").hide();
                    } else {
                        var nextLevel = data.data[0].category_level;
                        $("#classUl" + nextLevel + "").html(classStr);
                        $("#classUl" + level + "").show();
                    }
                }
            }
            web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
        }
        //获取光标
        $("#class" + level + "").focus(function() {
            $("#classUl" + level + "").show();
        });
        $(document).bind("click", function(e) {
            //id为menu的是菜单，id为open的是打开菜单的按钮
            if($(e.target).closest("#classUl" + level + "").length == 0 && $(e.target).closest("#class" + level + "").length == 0) {
                //点击id为menu之外且id不是不是open，则触发
                $("#classUl" + level + "").hide()
            }
        });
    }

}

function nextClass(category_id, level, category_name) {
    $("#class" + level + "").val(category_name);
    if(level == 1) {
        firstCategoryId = category_id;
        $("#class2").val("");
        $("#class3").val("");
    } else if(level == 2) {
        secondCategoryId = category_id;
        $("#class3").val("");
    }
    var user_data = {
        parentCateId: category_id,
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null) {
                classStr = "<li>无数据</li>";
            } else {
                var classStr = "";
                for(var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='nextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
            }
            if(data.data[0] == undefined) {
                classValue = category_id;
                var nextLevel = level + 1;
                $("#classUl" + nextLevel + "").html(classStr);
                $("#classUl" + level + "").hide();
            } else {
                var nextLevel = data.data[0].category_level;
                $("#classUl" + nextLevel + "").html(classStr);
                $("#classUl" + nextLevel + "").show();
                $("#classUl" + level + "").hide()
            }
        }
    }
    getNext_input(level + 1, category_id);
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}


function reloadSpuTable() {
    if ($('#ascription_lsit option:selected') .val()==""){

        layer.msg("请选择渠道商");
        return;
    }

    var customerType = $("input:checkbox[name='customerType']:checked").map(function(index,elem) {
        return $(elem).val();
    }).get().join(',');

    if(customerType.indexOf("1")>-1 && customerType.indexOf("2")>-1){
        customerType = 3;
    }
    var saleMsg = $("input:checkbox[name='saleArea']:checked").map(function(index,elem) {
        return $(elem).val();
    }).get().join(',');
    if(saleMsg == "" || saleMsg == undefined || saleMsg == null ){
        saleMsg = -1;
    }

    var categoryName = $("#class3").val();
    var firstCategoryName = $("#class1").val();
    var secondCategoryName = $("#class2").val();
    if(categoryName == "" || categoryName == undefined || categoryName == null){
        goodslist_cantegory_id = "";
        thirdCategoryId= "";
    }

    if(firstCategoryName == "" || firstCategoryName == undefined || firstCategoryName == null){
        firstCategoryId = "";
    }

    if(secondCategoryName == "" || secondCategoryName == undefined || secondCategoryName == null){
        secondCategoryId = "";
    }
    console.log($("#getArea").find("option:selected").val())
    if($("#getArea").find("option:selected").val()==0){
        layer.msg("请选择区域");
        return false;
    }
    $.ajax({
        contentType: 'application/json',
        url: root.dockingplatform.channerlPriceManage,
        cache: false, //禁用缓存
        type: "POST",
        data: JSON.stringify({
            "customerId":$("#ascription_lsit").val(),//渠道商id
            firstCategoryId : firstCategoryId,
            secondCategoryId :secondCategoryId,
            thirdCategoryId: goodslist_cantegory_id,
            spuCode:$("#spu_code").val(),
            skuCode:$("#Code").val(),
            helpCode:$("#helpcode").val(),
            brandName: $("#brandName").val(),
            skuName: $("#goodsName").val(),
            "areaId":$("#getArea").find("option:selected").val(),
            "areaName":$("#getArea").find("option:selected").html(),
            pageNum:1,
            pageSize:20,
        }),
        success: function(data) {
            if(data.data.basePriceType==1){
                $("#jiage").html("售价")
            }else if(data.data.basePriceType==2){
                $("#jiage").html("批发价")
            }else if(data.data.basePriceType==3){
                $("#jiage").html("签约价")
            }
            goodsList(data);
            $("#spu_all").attr("checked", false);
            totalPage = data.data.listData.pageCount;
            $('.M-box3').pagination({
                pageCount: totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    console.log(api.getCurrent());
                    $.ajax({
                        contentType: 'application/json',
                        url: root.dockingplatform.channerlPriceManage,
                        // url: "http://localhost:8080/product/product/spuList",
                        cache: false, //禁用缓存
                        type: "POST",
                        data: JSON.stringify({
                            "customerId":$("#ascription_lsit").val(),//渠道商id
                            firstCategoryId : firstCategoryId,
                            secondCategoryId :secondCategoryId,
                            thirdCategoryId: goodslist_cantegory_id,
                            spuCode:$("#spu_code").val(),
                            skuCode:$("#Code").val(),
                            helpCode:$("#helpcode").val(),
                            brandName: $("#brandName").val(),
                            skuName: $("#goodsName").val(),
                            "areaId":$("#getArea").find("option:selected").val(),
                            "areaName":$("#getArea").find("option:selected").html(),
                            pageNum:api.getCurrent(),
                            pageSize:20,
                        }),
                        success: function(data) {
                            goodsList(data);
                        }
                    });
                }
            });
        }
    });
}
//获取用户信息
var localStorageParameters = web.readCookie("loginData");
var JsonObject =JSON.parse(localStorageParameters);

var oneCategoryId = "";
var twoCategoryId = "";
var threeCategoryId = "";
// 2,3,4级的模糊查询
function imgGetNext_input(level, category_id) {
    if(level < 4) {
        $("#imgClass" + level + "")[0].oninput = function() {
            console.log(level)
            console.log($("#imgClass" + level + "").val())
            if(level == 1) {
                oneCategoryId = category_id;
            } else if(level == 2) {
                twoCategoryId = category_id;
            } else if(level == 3) {
                threeCategoryId = category_id;
            }
            var user_data = {
                parentCateId: category_id,
                categoryName: $("#imgClass" + level + "").val()
            }
            var succCallBack = function(data, status, response) {
                console.log(data)
                if(data.errorCode == "0") {
                    if(data.data == null) {
                        classStr = "<li>无数据</li>";
                    } else {
                        var classStr = "";
                        for(var i = 0; i < data.data.length; i++) {
                            classStr += "<li onclick='imgNextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                        }
                    }
                    if(data.data[0] == undefined) {
                        classValue = category_id;
                        var nextLevel = level + 1;
                        $("#img-classUl" + nextLevel + "").html(classStr);
                        $("#img-classUl" + level + "").hide();
                    } else {
                        var nextLevel = data.data[0].category_level;
                        $("#img-classUl" + nextLevel + "").html(classStr);
                        $("#img-classUl" + level + "").show();
                    }
                }
            }
            web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
        }
        //获取光标
        $("#imgClass" + level + "").focus(function() {
            $("#img-classUl" + level + "").show();
        });
        $(document).bind("click", function(e) {
            //id为menu的是菜单，id为open的是打开菜单的按钮
            if($(e.target).closest("#img-classUl" + level + "").length == 0 && $(e.target).closest("#imgClass" + level + "").length == 0) {
                //点击id为menu之外且id不是不是open，则触发
                $("#img-classUl" + level + "").hide()
            }
        });
    }

}

function imgNextClass(category_id, level, category_name) {
    console.log(level)
    console.log(category_id)
    if(level == 1) {
        $("#imgClass2").val("");
        $("#imgClass3").val("");
    } else if(level == 2) {
        $("#imgClass3").val("");
    }
    if(level == 1) {
        oneCategoryId = category_id;
    } else if(level == 2) {
        twoCategoryId = category_id;
    } else if(level == 3) {
        threeCategoryId = category_id;
    }
    $("#imgClass" + level + "").val(category_name)
    var user_data = {
        parentCateId: category_id
    }
    var succCallBack = function(data, status, response) {
        console.log(data)
        if(data.errorCode == "0") {
            if(data.data == null) {
                classStr = "<li>无数据</li>";
            } else {
                var classStr = "";
                for(var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='imgNextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
            }
            if(data.data[0] == undefined) {
                classValue = category_id;
                var nextLevel = level + 1;
                $("#img-classUl" + nextLevel + "").html(classStr);
                $("#img-classUl" + level + "").hide();
            } else {
                var nextLevel = data.data[0].category_level;
                $("#img-classUl" + nextLevel + "").html(classStr);
                $("#img-classUl" + nextLevel + "").show();
                $("#img-classUl" + level + "").hide()
            }
        }
    }
    imgGetNext_input(level + 1, category_id);
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}
var totalPage = 1;


